package com.baylor.server.business.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baylor.server.base.util.DynamicSpecifications;
import com.baylor.server.base.util.SearchFilter;
import com.baylor.server.business.entity.UserInfo;
import com.baylor.server.business.repository.CioeCompanyRespository;
import com.baylor.server.business.repository.UserInfoRespository;
import com.baylor.server.business.service.UserInfoService;
 
@Service
@Transactional(readOnly = true)
public class UserInfoServiceImpl implements UserInfoService {
	@Autowired
	private UserInfoRespository userInfoRespository;
	@Autowired
	private CioeCompanyRespository cioeCompanyRespository;

	public Page<UserInfo> findUserDataByFilter(
			Map<String, Object> filterParams, Pageable pageable) {
		Map<String, SearchFilter> filters = SearchFilter.parse(filterParams);
		return userInfoRespository.findAll(DynamicSpecifications
				.bySearchFilter(filters.values(), UserInfo.class), pageable);
	}

	@Transactional(readOnly = false)
	public UserInfo save(UserInfo user) {
		user.setUserRegister(new SimpleDateFormat("yyyy-MM-dd HH:MM:ss")
				.format(new Date()));
		user.setUserState(0);// 0：未登录
		user.setEntraceState("否");
		return userInfoRespository.save(user);
	}
	@Transactional(readOnly = false)
	public UserInfo saveUserPrizeCount(UserInfo user) {
 
		return userInfoRespository.save(user);
	}
	@Transactional(readOnly = false)
	public UserInfo userForbid(Integer userId) {
		UserInfo user = userInfoRespository.findOne(userId);
		user.setUserState(3);
		return userInfoRespository.save(user);
	}

	@Transactional(readOnly = false)
	public UserInfo userAllow(Integer userId) {
		UserInfo user = userInfoRespository.findOne(userId);
		user.setUserState(0);
		return userInfoRespository.save(user);
	}

	public UserInfo findOne(Integer userId) {
		return userInfoRespository.findOne(userId);
	}

	@Transactional(readOnly = false)
	public UserInfo userUpdateByUserIdSave(UserInfo user) {
		UserInfo userdb = userInfoRespository.findOne(user.getUserId());
		userdb.setUserName(user.getUserName());
		userdb.setUserType(user.getUserType());
		userdb.setUserSex(user.getUserSex());
		userdb.setUserPhone(user.getUserPhone());
		userdb.setUserEmail(user.getUserEmail());
		userdb.setIndustry(user.getIndustry());
		userdb.setUserPhoto(user.getUserPhoto());
		userdb.setUserCompany(user.getUserCompany()); 
		userdb.setArea(user.getArea());
		userdb.setGoal(user.getGoal());
		return userInfoRespository.save(userdb);
	}


	@Transactional(readOnly = false)
	public UserInfo userRegister(UserInfo user) {
		user.setEntraceState("否");
		user.setUserState(2);
 
        user.setPrizeNum(1f);
		user.setUserRegister(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
 
		return userInfoRespository.save(user);
	}

	public UserInfo findUserByPhone(String userPhone) {
		 List<UserInfo> list=userInfoRespository.findUserByPhone(userPhone);
		 UserInfo user=new UserInfo();
		 if(list!=null&&list.size()>0)
		 {
			 user=list.get(0);
		 } 
		return user;
 
	}

	@Transactional(readOnly = false)
	public UserInfo update(UserInfo userdb) {
		return userInfoRespository.save(userdb);
	}

	public UserInfo findUserByPhoneAndEmail(String userEmail, String userPhone) {

		return userInfoRespository
				.findUserByPhoneAndEmail(userEmail, userPhone);
	}

	@Transactional(readOnly = false)
	public UserInfo userPwdChuShiHua(Integer userId) {
		UserInfo user = userInfoRespository.findOne(userId);

		user.setUserPassword(user.getUserPhone().substring(
				user.getUserPhone().length() - 6, user.getUserPhone().length()));
		return userInfoRespository.save(user);
	}
	
	/**
	 * 查询重复邮箱
	 */
	@Transactional(readOnly = false)
	public UserInfo findEmail(String userEmail) {
		 List<UserInfo> list=userInfoRespository.findEmail(userEmail);
		 UserInfo user=new UserInfo();
		 if(list!=null&&list.size()>0)
		 {
			 user=list.get(0);
		 } 
		return user;
	}


	public List<UserInfo> findUserInfoListByCondition(String startTime,
			String endTime,String userType) {
		
		return userInfoRespository.getUserByTime(startTime, endTime,userType);
	}

	@Transactional(readOnly=false)
	public void delUserInfo(int id) {
		userInfoRespository.delete(id);
	}

    

}
